//index.js
//获取应用实例
const api = require('../../../utils/api') // 引入API
const {myRequest} = require('../../../utils/myRequest') // 请求函数
const WxParse = require('../../../libs/wxParse/wxParse.js') // 富文本解析
Page({
  data: {
    banner: [], // 轮播图数据
    channel: [], // 频道数据
    brandList: [], // 频道数据
    categoryList: [], // 分类数据
    hotGoodsList: [], // 热卖数据
    newGoodsList: [], // 新品数据
    topicList: [], // 话题数据
    switchItem: 0,
    goodsList: [], // 右边栏数据
    id: 1005000,  // 初始渲染第一栏数据
  },
  onLoad: function (options) {
    // 首页数据
    myRequest({
      url: api.index,
      success: (res)=>{
        console.log(res);
        let banner = res.data.data.banner;
        let channel = res.data.data.channel;
        let newGoodsList = res.data.data.newGoodsList;
        this.setData({
          banner: banner,  // 轮播图数据
          channel: channel,  // 频道
          newGoodsList: newGoodsList,
        })
      },
    });
  },
  // 请求函数
  indexPagesData() {
    myRequest({
      url: api.index,
      success: (res)=>{
        console.log(res);
        let banner = res.data.data.banner;
        let brandList = res.data.data.brandList;
        let categoryList = res.data.data.categoryList;
        let goodsList = res.data.data.categoryList[0].goodsList;
        let hotGoodsList = res.data.data.hotGoodsList;       
        let topicList = res.data.data.topicList;
        this.setData({
          banner: banner,  // 轮播图数据
          brandList: brandList,
          categoryList: categoryList,
          hotGoodsList: hotGoodsList,
          topicList: topicList,
          goodsList: goodsList
        })
        WxParse.wxParse('content', 'html', topicList.content, this, 5)
      },
    });
  },
  // 切换
  selectItem(e) {
    let item = e.target.dataset.item;
    this.setData({
      switchItem: item
    })
    this.indexPagesData()
  },
})
